<template>
  <!-- 登录页面 -->
  <div>
    <Row class="container">
      <i-col span="13" offset="2" class="login-img-box">
        <img src="static/img/sale.jpg" alt="">
      </i-col>
      <i-col span="7" class="login-box">
        <div class="login-container">
          <div class="login-header">
            <p>欢迎登录</p>
          </div>
          <div class="form-box">
            <el-form
              v-loading="loading"
              :model="ruleForm"
              status-icon
              :rules="rules"
              ref="ruleForm"
              label-width="100px"
            >
              <el-form-item label="账号" prop="account">
                <el-input v-model="ruleForm.account"></el-input>
              </el-form-item>

              <el-form-item label="密码" prop="password">
                <el-input
                  type="password"
                  v-model="ruleForm.password"
                  autocomplete="off"
                ></el-input>
              </el-form-item>

              <el-form-item label="记住" prop="rememberMe">
                <el-switch v-model="ruleForm.rememberMe" active-color="#ffad33"></el-switch>
              </el-form-item>

              <el-form-item>
                <el-button type="warning" @click="submitForm('ruleForm')">提交</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </i-col>
    </Row>
  </div>
</template>

<script>
import {mapActions} from 'vuex';
// import store from '@/vuex/store';
export default {
  name: 'Login',
  data () {
    return {
      loading: false,
      ruleForm: {
        account: '',
        password: '',
        rememberMe: true
      },
      rules: {
        account: [
          {required: true, message: '请输入账号', trigger: 'blur'},
          {
            min: 2,
            max: 15,
            message: '长度在 2 到 15 个字符',
            trigger: 'blur'
          }
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {
            min: 6,
            max: 20,
            message: '长度在 6 到 20 个字符',
            trigger: 'blur'
          }
        ]
      }
    };
  },
  methods: {
    ...mapActions(['login', 'getInfo']),
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch('login', this.ruleForm)
            // .then((value) => {
            .then(() => {
              // console.log(value);
              // const {code, message} = value;
              // if (code === 200) {
              this.$message({
                message: '恭喜你，登录成功',
                type: 'success',
                duration: 2000
              });
              this.$store.dispatch('getInfo');
              setTimeout(() => {
                this.loading = false;
                this.$router.push({ path: this.redirect || '/' });
              }, 0.1 * 1000);
              // } else {
              //   this.$message.error('注册失败，' + message);
              // }
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          return false;
        }
      });
    },
    resetForm (formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style scoped>
.container {
  margin-top: 30px;
  height: 485px;
  background-color: #fff;
}

.login-img-box {
  height: 485px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-img-box > img {
  width: 68%;
}

.login-box {
  height: 485px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-container {
  width: 80%;
  height: 280px;
  border: #f3c913;
}

.login-header {
  height: 50px;
  font-size: 20px;
  text-align: center;
  line-height: 50px;
  letter-spacing: 5px;
  color: #fff;
  background-color: #ffad33;
}

.form-box {
  width: 80%;
  margin: 30px auto;
}
</style>
